/*
 * FILE: Value_Chart_Alert.mq4 
 * AUTHOR: Thierry Chappuis <tc77@pygnol.ch>
 * DESCRIPTION:
 * MT4 Custom indicator 
 * LOG:
 * - 20121104: first version
 *
 * Copyright 2012, Thierry Chappuis                                                          
 */

#property copyright "Copyright 2012, Thierry Chappuis"

#property indicator_chart_window

/* Input parameters */
extern int numBars = 5;

extern int nLevels = 4;
extern double level1 = 6;
extern double level2 = -6;
extern double level3 = 8;
extern double level4 = -8;
extern double level5 = 10;
extern double level6 = -10;

extern double exitSig = 0.1;

double levels[6];
bool in[6];

int init()
{
   levels[0] = level1;
   levels[1] = level2;
   levels[2] = level3;
   levels[3] = level4;
   levels[4] = level5;
   levels[5] = level6;
   
   for (int i = 0; i < nLevels; i++)
   {
      in[i] = true;
   }
   
   return(0);
}

int start()
{
   double relativeClose;
   double sum;
   double floatingAxis;
   double volatilityUnit;
   
   // Computation of the floating axis value
   sum = 0;
   for (int i = 0; i < numBars; i++)
   {
      sum += (High[i] + Low[i]) / 2.0;
   }
      
   floatingAxis = sum / numBars;
   
   // Computation of the volatility unit value
   sum = 0;
   for (i = 0; i < numBars; i++)
   { 
      sum += High[i] - Low[i];
   }
   
   volatilityUnit = 0.2 * (sum / numBars);
   
   // Computation of the relative close value
   relativeClose = (Close[0] - floatingAxis) / volatilityUnit;
   
   // Alert system   
   for (i = 0; i < nLevels; i++)
   {
      double level = levels[i];
      
      if (level > 0)
      {
         if (relativeClose >= level && in[i] == false)
         {
            in[i] = true;
            Alert(Symbol(), " (", Period(), " min) signal: Value chart above ", level, "!");
         }
         
         if (relativeClose < level - exitSig && in[i] == true)
         {
            in[i] = false;
         }
      }
      
      if (level < 0)
      {
         if (relativeClose <= level && in[i] == false)
         {
            in[i] = true;
            Alert(Symbol(), " (", Period(), " min) signal: Value chart below ", level, "!");
         }     
         
         if (relativeClose > level + exitSig && in[i] == true)
         {
            in[i] = false;
         }  
      }
   }                                                                                  

   return(0);
}

